﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
using System.Data;
using System.Data.SqlClient;
using QuanLyRapChieuPhim.DTO;
using System.ComponentModel;

namespace QuanLyRapChieuPhim.DAO
{
    class CaChieuDAO
    {
        DataProvider dp = new DataProvider();

        public BindingList<CaChieuDTO> GetList()
        {
            
            DataTable dt = new DataTable();
            BindingList<CaChieuDTO> ds = new BindingList<CaChieuDTO>();
            if (dp.OpenConnect())
            {
                dt = dp.SqlExcuteQuery("uproc_GetList_CaChieu", null);
                dp.CloseConnect();
                foreach (DataRow row in dt.Rows)
                {
                    CaChieuDTO CaChieu = new CaChieuDTO();
                    CaChieu.MaCaChieu = int.Parse(row["maCaChieu"].ToString());
                    CaChieu.TenCaChieu = row["tenCaChieu"].ToString();
                    CaChieu.ThoiGianBatDau = TimeSpan.Parse(row["thoiGianBatDau"].ToString());
                    CaChieu.ThoiGianKetThuc = TimeSpan.Parse(row["thoiGianKetThuc"].ToString());
                    ds.Add(CaChieu);
                }
                return ds;
            }
            return null;    
        }

        public BindingList<CaChieuDTO> GetList_QL()
        {

            DataTable dt = new DataTable();
            BindingList<CaChieuDTO> ds = new BindingList<CaChieuDTO>();
            if (dp.OpenConnect())
            {
                dt = dp.SqlExcuteQuery("uproc_GetList_CaChieu_QL", null);
                dp.CloseConnect();
                foreach (DataRow row in dt.Rows)
                {
                    CaChieuDTO CaChieu = new CaChieuDTO();
                    CaChieu.MaCaChieu = int.Parse(row["maCaChieu"].ToString());
                    CaChieu.TenCaChieu = row["tenCaChieu"].ToString();
                    CaChieu.ThoiGianBatDau = TimeSpan.Parse(row["thoiGianBatDau"].ToString());
                    CaChieu.ThoiGianKetThuc = TimeSpan.Parse(row["thoiGianKetThuc"].ToString());
                    CaChieu.DaXoa = int.Parse(row["DaXoa"].ToString());
                    ds.Add(CaChieu);
                }
                return ds;
            }
            return null;
        }

        public BindingList<CaChieuDTO> GetList_QL_DaXoa()
        {

            DataTable dt = new DataTable();
            BindingList<CaChieuDTO> ds = new BindingList<CaChieuDTO>();
            if (dp.OpenConnect())
            {
                dt = dp.SqlExcuteQuery("uproc_GetList_CaChieu_QL_DaXoa", null);
                dp.CloseConnect();
                foreach (DataRow row in dt.Rows)
                {
                    CaChieuDTO CaChieu = new CaChieuDTO();
                    CaChieu.MaCaChieu = int.Parse(row["maCaChieu"].ToString());
                    CaChieu.TenCaChieu = row["tenCaChieu"].ToString();
                    CaChieu.ThoiGianBatDau = TimeSpan.Parse(row["thoiGianBatDau"].ToString());
                    CaChieu.ThoiGianKetThuc = TimeSpan.Parse(row["thoiGianKetThuc"].ToString());
                    CaChieu.DaXoa = int.Parse(row["DaXoa"].ToString());
                    ds.Add(CaChieu);
                }
                return ds;
            }
            return null;
        }

        public BindingList<CaChieuDTO> GetListLikeName_QL(String _Ten)
        {

            DataTable dt = new DataTable();
            BindingList<CaChieuDTO> ds = new BindingList<CaChieuDTO>();
            if (dp.OpenConnect())
            {
                SqlParameter[] sqlParameter = 
                { 
                    new SqlParameter("@Ten", _Ten) 
                };
                dt = dp.SqlExcuteQuery("uproc_GetListLikeName_CaChieu_QL", sqlParameter);
                foreach (DataRow row in dt.Rows)
                {
                    CaChieuDTO CaChieu = new CaChieuDTO();
                    CaChieu.MaCaChieu = int.Parse(row["maCaChieu"].ToString());
                    CaChieu.TenCaChieu = row["tenCaChieu"].ToString();
                    CaChieu.ThoiGianBatDau = TimeSpan.Parse(row["thoiGianBatDau"].ToString());
                    CaChieu.ThoiGianKetThuc = TimeSpan.Parse(row["thoiGianKetThuc"].ToString());
                    CaChieu.DaXoa = int.Parse(row["DaXoa"].ToString());
                    ds.Add(CaChieu);
                }
                dp.CloseConnect();
                return ds;
            }
            return null;
        }

        public BindingList<CaChieuDTO> GetListLikeName_QL_DaXoa(String _Ten)
        {

            DataTable dt = new DataTable();
            BindingList<CaChieuDTO> ds = new BindingList<CaChieuDTO>();
            if (dp.OpenConnect())
            {
                SqlParameter[] sqlParameter = 
                { 
                    new SqlParameter("@Ten", _Ten) 
                };
                dt = dp.SqlExcuteQuery("uproc_GetListLikeName_CaChieu_QL_DaXoa", sqlParameter);
                foreach (DataRow row in dt.Rows)
                {
                    CaChieuDTO CaChieu = new CaChieuDTO();
                    CaChieu.MaCaChieu = int.Parse(row["maCaChieu"].ToString());
                    CaChieu.TenCaChieu = row["tenCaChieu"].ToString();
                    CaChieu.ThoiGianBatDau = TimeSpan.Parse(row["thoiGianBatDau"].ToString());
                    CaChieu.ThoiGianKetThuc = TimeSpan.Parse(row["thoiGianKetThuc"].ToString());
                    CaChieu.DaXoa = int.Parse(row["DaXoa"].ToString());
                    ds.Add(CaChieu);
                }
                dp.CloseConnect();
                return ds;
            }
            return null;
        }

        public CaChieuDTO GetFromId(int _Ma )
        {
            DataTable dt = new DataTable();
            if (dp.OpenConnect())
            {
                SqlParameter[] sqlParameter = { new SqlParameter("@Ma",_Ma)};
                dt = dp.SqlExcuteQuery("uproc_GetFromID_CaChieu", sqlParameter);
                dp.CloseConnect();
                CaChieuDTO CaChieu = new CaChieuDTO();
                foreach (DataRow row in dt.Rows)
                {
                    CaChieu.MaCaChieu = int.Parse(row["maCaChieu"].ToString());
                    CaChieu.TenCaChieu = row["tenCaChieu"].ToString();
                    CaChieu.ThoiGianBatDau = TimeSpan.Parse(row["thoiGianBatDau"].ToString());
                    CaChieu.ThoiGianKetThuc = TimeSpan.Parse(row["thoiGianKetThuc"].ToString());
                }
                return CaChieu;
            }
            return null;
        }

        public bool Delete(int _Ma)
        {
            if (dp.OpenConnect())
            {
                SqlParameter[] sqlParameter = 
                { 
                    new SqlParameter("@Ma", _Ma) 
                };
                bool kq = dp.SqlExcuteNonQuery("uproc_Delete_CaChieu", sqlParameter);
                dp.CloseConnect();
                return kq;
            }
            return false;
        }

        public bool UnDelete(int _Ma)
        {
            if (dp.OpenConnect())
            {
                SqlParameter[] sqlParameter = 
                { 
                    new SqlParameter("@Ma", _Ma) 
                };
                bool kq = dp.SqlExcuteNonQuery("uproc_UnDelete_CaChieu", sqlParameter);
                dp.CloseConnect();
                return kq;
            }
            return false;
        }

        public bool Add(CaChieuDTO _DTO)
        {
            if (dp.OpenConnect())
            {
                SqlParameter[] sqlParameter = 
                { 
                    new SqlParameter("@TenCaChieu", _DTO.TenCaChieu), 
                    new SqlParameter("@ThoiGianBatDau", _DTO.ThoiGianBatDau), 
                    new SqlParameter("@ThoiGianKetThuc", _DTO.ThoiGianKetThuc) 
                };
                bool kq = dp.SqlExcuteNonQuery("uproc_Add_CaChieu", sqlParameter);
                dp.CloseConnect();
                return kq;
            }
            return false;

        }

        public bool CheckExists(int _Ma,string _Ten)
        {
            if (dp.OpenConnect())
            {
                SqlParameter[] sqlParameter = 
                { 
                    new SqlParameter("@Ma", _Ma), 
                    new SqlParameter("@TenCaChieu", _Ten)
                };
                DataTable dt = dp.SqlExcuteQuery("uproc_CheckExists_CaChieu", sqlParameter);
                dp.CloseConnect();
                if (dt.Rows.Count == 0)
                    return false;
                return true;// đã tồn tại
            }
            return false;

        }

        public bool Update(CaChieuDTO _DTO)
        {
            if (dp.OpenConnect())
            {
                SqlParameter[] sqlParameter = 
                { 
                    new SqlParameter("@MaCaChieu", _DTO.MaCaChieu), 
                    new SqlParameter("@TenCaChieu", _DTO.TenCaChieu), 
                    new SqlParameter("@ThoiGianBatDau", _DTO.ThoiGianBatDau), 
                    new SqlParameter("@ThoiGianKetThuc", _DTO.ThoiGianKetThuc) 
                };
                bool kq = dp.SqlExcuteNonQuery("uproc_Update_CaChieu", sqlParameter);
                dp.CloseConnect();
                return kq;
            }
            return false;
        }
        
    }
}
